<?php

namespace App;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class Awardlog extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'receiver','tele','address','zip_code','award_id','consumer_id','status'
    ];
    public function consumer(){
        return $this->belongsTo(Consumer::class);
    }
    public function award(){
        return $this->belongsTo(Award::class);
    }
    public function keytodata($keyname){
        return collect($keyname)->map(function($key){
            if(  !is_string($this->$key) && !is_int($this->$key)){
                if(  $this->$key  instanceof Carbon){//判断是否是时间类
                    return [$key=>$this->$key->toFormattedDateString()];
                }
                else{
                    if($key == 'award'){
                        return [$key=>$this->$key->name];
                    }
                    return [$key=>$this->$key->nickname];
                }
            }
            return [$key=>$this->$key];
        })->collapse()->toArray();
    }
}